package com.zdp.provider.controller;

import com.zdp.provider.service.CourseService;
import com.zdp.provider.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.HashMap;
import java.util.List;

/**
 * 类描述: 教师信息服务
 *
 * @author ZhhengDP
 * @since 2021/8/16
 */
@RestController
@RequestMapping("/teacher_info")
public class TeacherController {

    private RestTemplate ribbonRestTemplate;

    private TeacherService teacherService;

    private CourseService courseService;

    private LoadBalancerClient ribbonClient;

    private RestTemplate restTemplate;

    @Autowired
    public TeacherController(@Qualifier("ribbonRestTemplate") RestTemplate ribbonRestTemplate,
                             TeacherService teacherService,
                             CourseService courseService,
                             LoadBalancerClient ribbonClient,
                             RestTemplate restTemplate) {
        this.ribbonRestTemplate = ribbonRestTemplate;
        this.teacherService = teacherService;
        this.courseService = courseService;
        this.ribbonClient = ribbonClient;
        this.restTemplate = restTemplate;
    }

    @GetMapping("/teacher")
    public TeacherVo getTeacher(Integer teacherId){
         TeacherVo teacher = teacherService.getTeacherById(teacherId);
         List<CourseDto> courses = courseService.getCourses(teacherId);
         teacher.setCourses(courses);
         return teacher;
    }

    @GetMapping("/teacherEx")
    public TeacherVo getTeacherEx(Integer teacherId){
        TeacherVo teacher = teacherService.getTeacherById(teacherId);
        List<CourseDto> courses = loadCoursesEx(teacherId);
        teacher.setCourses(courses);
        return teacher;
    }

    @GetMapping("/teacherExample")
    public TeacherVo getTeacherExample(Integer teacherId){
        TeacherVo teacher = teacherService.getTeacherById(teacherId);
        List<CourseDto> courses = loadCourses(teacherId);
        teacher.setCourses(courses);
        return teacher;
    }


    protected List<CourseDto> loadCourses(Integer teacherId){
        // 使用服务名来进行服务的调用
        String url = "http://courseService/course_info/courses?teacherId={teacherId}";
        HashMap<String,Object> param = new HashMap<>();
        param.put("teacherId",teacherId);
        List<CourseDto> result = ribbonRestTemplate.getForObject(url, List.class, param);
        return result;
    }

    protected  List<CourseDto> loadCoursesEx(Integer teacherId){
        /**
         * 首先是通过 RibboClient来获取到CourseService的一个微服务实例
         * choose方法将根据所配置的客户端负载均衡策略从CourseService微服务实例中选择其中一个
         * */
        ServiceInstance instance = this.ribbonClient.choose("courseService");
        /**
         * 通过RestTemplate进行请求
         * */
        URI userUri = URI.create(String.format("http://%s:%s/course_info/courses", instance.getHost(), instance.getPort()));
        HashMap<String,Object> param = new HashMap<>();
        param.put("teacherId",teacherId);
        String url = userUri.toString() + "?teacherId={teacherId}";
        List<CourseDto> result = restTemplate.getForObject(url,List.class,param);
        return result;
    }
}
